package com.mall.admin.controller;

import com.mall.admin.service.PmsProductAttributeCategoryService;
import com.mall.commons.utils.PageResult;
import com.mall.commons.utils.R;
import com.mall.domain.PmsProductAttributeCategory;
import com.mall.vo.PmsProductAttributeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/productAttribute/category")
public class PmsProductAttributeCategoryController {

    @Autowired
    private PmsProductAttributeCategoryService pmsProductAttributeCategoryService;

    /**
     * 添加商品属性分类
     *
     * @param name
     * @return
     */
    @PostMapping("/create")
    public R create(String name) {
        pmsProductAttributeCategoryService.create(name);
        return R.success(null);
    }

    /**
     * 获取所有商品属性分类及其下属性
     *
     * @return
     */
    @GetMapping("/list/withAttr")
    public R listWithAttr() {
        List<PmsProductAttributeVo> list = pmsProductAttributeCategoryService.listWithAttr();
        return R.success(list);
    }

    /**
     * 分页获取所有商品属性分类
     *
     * @return
     */
    @GetMapping("/list")
    public R list(@RequestParam(defaultValue = "1") Integer pageNum,
                  @RequestParam(defaultValue = "10") Integer pageSize) {
        PageResult pageResult = pmsProductAttributeCategoryService.list(pageNum, pageSize);
        return R.success(pageResult);
    }

    /**
     * 删除单个商品属性分类
     * @param id
     * @return
     */
    @GetMapping("/delete/{id}")
    public R deleteById(@RequestParam Integer id){
        pmsProductAttributeCategoryService.deleteById(Long.valueOf(id));
        return R.success(null);
    }

    /**
     * 修改商品属性分类
     * @param id
     * @param name
     * @return
     */
    @PostMapping("/update/{id}")
    public R updateById(@PathVariable Integer id,String name){
        pmsProductAttributeCategoryService.updateById(Long.valueOf(id),name);
        return R.success(null);
    }

    /**
     * 获取单个商品属性分类信息
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public R attributeCategoryById(@PathVariable Integer id){
        PmsProductAttributeCategory category=pmsProductAttributeCategoryService.attributeCategoryById(Long.valueOf(id));
        return R.success(category);
    }
}
